package service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static util.DBUtil.conn;

// 放映厅服务类
public class ScreeningRoomService {

    public static void updateRoom(String id, String name, int capacity) {
        try (PreparedStatement ptmt = conn.prepareStatement("UPDATE rooms SET name=?,capacity=? WHERE id=?");) {
            ptmt.setString(1, name);
            ptmt.setInt(2, capacity);
            ptmt.setString(3, id);
            ptmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void deleteRoom(String username, String id) {
        // 先查询放映厅名称
        String roomName = null;

        try (PreparedStatement selectStmt = conn.prepareStatement("SELECT name FROM rooms WHERE id = ?")) {
            selectStmt.setString(1, id);
            try (ResultSet rs = selectStmt.executeQuery()) {
                if (rs.next()) {
                    roomName = rs.getString("name");
                } else {
                    System.out.println("未找到ID为" + id + "的放映厅");
                    return;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询放映厅失败", e);
        }

        // 执行删除操作

        try (PreparedStatement deleteStmt = conn.prepareStatement("DELETE FROM rooms WHERE id = ?")) {
            deleteStmt.setString(1, id);
            int rowsAffected = deleteStmt.executeUpdate(); // 正确使用executeUpdate()
            if (rowsAffected > 0) {
                LogService.logAction(username, "删除放映厅: " + roomName);
            } else {
                System.out.println("删除失败，未影响任何记录");
            }
        } catch (SQLException e) {
            throw new RuntimeException("删除放映厅失败", e);
        }
    }

    public static String getRoomById(String id) {
        try (PreparedStatement ptmt = conn.prepareStatement("SELECT room FROM screening WHERE id=?");) {
            ptmt.setString(1, id);
            ResultSet rs = ptmt.executeQuery();
            if (rs.next()) {
                return rs.getString("room");
            } else {
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int getCapacityById(String id) {
        try (PreparedStatement ptmt = conn.prepareStatement("SELECT capacity FROM rooms WHERE id=?");) {
            ptmt.setString(1, id);
            ResultSet rs = ptmt.executeQuery();
            if (rs.next()) {
                return rs.getInt("capacity");
            } else {
                return 0;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int getCapacityByName(String name) {
        try (PreparedStatement ptmt = conn.prepareStatement("SELECT capacity FROM rooms WHERE name=?");) {
            ptmt.setString(1, name);
            ResultSet rs = ptmt.executeQuery();
            if (rs.next())
                return rs.getInt("capacity");
            else
                return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static double getPriceId(String screeningId) {
        try (PreparedStatement ptmt = conn.prepareStatement("SELECT price FROM screening WHERE id=?");) {
            ptmt.setString(1, screeningId);
            ResultSet rs = ptmt.executeQuery();
            if (rs.next())
                return rs.getDouble("price");
            else
                return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
